import { homeRouterMap, constantRouterMap, schoolRouterMap, schoolAffairRouterMap, fundRouterMap, operationRouterMap, teachingRouterMap, systemRouterMap, userManageRouterMap } from 'src/router';

/**
 * 是否有权限访问路由
 * 
 * @param {array} roles 角色
 * @param {array} route 路由
 * @returns {boolean} 是否有权限
 */

// 递归路由与数据
function filterRoute(routeList, filterList) {
    var ret = routeList.filter((item, i) => {
        let fItem = null;
        filterList.forEach(f => {
            if (f.path == item.path) {
                fItem = f;
            }
        });
        if (fItem) {
            if (item.children) {
                if (fItem.children) {
                    if (fItem.markedLvl > 1) {
                        filterRoute(item.children, fItem.children);
                    }
                    // filterRoute(item.children, fItem.children);
                } else {
                    delete item.children;
                }
            }
            return true;
        } else {
            return false;
        }
    });
    routeList.length = 0;
    ret.forEach(item => routeList.push(item));
}

function fixHomePath(routerMap) {
    let first = routerMap[1];
    let second = first.children[0].path;
    let redirect;
    if (second[0] === '/') redirect = second;
    else redirect = first.path + '/' + second;
    routerMap[0].redirect = redirect;
}

function fixRedirect(routerMap) { // 统一更新item的redirect为children第一项
    routerMap.forEach(item => {
        if (item.children) {
            if (item.redirect) {
                if (item.children.length == 0) return
                if (item.children[0].path[0] == '/') {
                    item.redirect = item.children[0].path;
                } else {
                    item.redirect = item.path + '/' + item.children[0].path;
                }
            }
            fixRedirect(item.children);
        }
    });
}

// 展示路由层级 传过来3
function markLvl(filterList, lvl) {
    filterList.forEach(m => {
        if (lvl > 0) {
            m.markedLvl = lvl;
            if (m.children) {
                markLvl(m.children, lvl - 1);
            }
        }
    });
}

const permission = {
    state: {
        routers: constantRouterMap,
        addRouters: [],
        systems: []
    },

    mutations: {
        SET_ROUTERS: (state, routers) => {
            state.addRouters = routers;
            state.routers = constantRouterMap.concat(routers);
        },
        SET_SYSTEM: (state, routers) => {
            state.systems = routers;
        }
    },

    actions: {
        GenerateRoutes({ commit }, data) {
            /* global Promise */
            return new Promise(resolve => {
                // const routeBase = location.pathname.substr(1);
                const routeBase = location.search.substr(1);
                let routerMap;
                switch (routeBase) {
                    case 'school':
                        routerMap = schoolRouterMap;
                        break;
                    case 'schoolAffair':
                        routerMap = schoolAffairRouterMap;
                        break;
                    case 'funds':
                        routerMap = fundRouterMap;
                        break;
                    case 'operation':
                        routerMap = operationRouterMap;
                        break;
                    case 'teaching':
                        routerMap = teachingRouterMap;
                        break;
                    case 'system':
                        routerMap = systemRouterMap;
                        break;
                    case 'userManage':
                        routerMap = userManageRouterMap;
                        break;
                    default:
                        routerMap = homeRouterMap;
                }
                if (routerMap !== homeRouterMap) {
                    data.menus.push({ path: '/' });
                    // 设置路由展示层级
                    markLvl(data.menus, 3);
                    // edit by cuihd at 20180816 暂时隐藏提测时需要放开
                    filterRoute(routerMap, data.menus);
                    fixHomePath(routerMap);
                    fixRedirect(routerMap);
                }
                routerMap.push({ path: '*', redirect: '/404', hidden: true });
                commit('SET_SYSTEM', data.subsys);
                commit('SET_ROUTERS', routerMap);
                resolve();
            })
        }
        /*
        GenerateRoutes({ commit }, data) {
            return new Promise(resolve => {
                const { roles } = data;
                // const routeBase = location.pathname.substr(1);
                const routeBase = location.search.substr(1);

                let routerMap;
                switch (routeBase) {
                    case 'school':
                        routerMap = schoolRouterMap;
                        break;
                    case 'schoolAffair':
                        routerMap = schoolAffairRouterMap;
                        break;
                    case 'funds':
                        routerMap = fundRouterMap;
                        break;
                    case 'operation':
                        routerMap = operationRouterMap;
                        break;
                    case 'teaching':
                        routerMap = teachingRouterMap;
                        break;
                    case 'system':
                        routerMap = systemRouterMap;
                        break;
                    case 'userManage':
                        routerMap = userManageRouterMap;
                        break;    
                    default:
                        routerMap = homeRouterMap;
                }
                const accessedRouters = routerMap.filter(v => {
                    if (roles.indexOf('admin') >= 0) return true;
                    if (hasPermission(roles, v)) {
                        if (v.children && v.children.length > 0) {
                            v.children = v.children.filter(child => {
                                if (hasPermission(roles, child)) {
                                    return child
                                }
                                return false;
                            });
                            return v
                        } else {
                            return v
                        }
                    }
                    return false;
                });
                commit('SET_ROUTERS', accessedRouters);
                resolve();
            })
        }
        */
    }
};


export default permission;
